package com.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.entity.YoukuVideo;

public class DBConnect {
	private final String DB_GET_SHARPNESS = "name = 'count'";
	private static final String DB_TABLE = "setting";
	private static final String DB_HISTORY_TABLE = "history";
	public static final String HISTORY_KEY_TITLE = "title";
	public static final String HISTORY_KEY_VID = "vid";
	public static final String HISTORY_KEY_IMG = "img";
	public static final String HISTORY_KEY_PLAYTIME = "playtime";
	public static final String HISTORY_KEY_DURATION = "duration";
	private Context context;
	private final String DB_NAME = "video.db";
	private static final int DB_VERSION = 1;

	private DBOpenHelper dbOpenHelper;
	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_VALUE = "value";
	private SQLiteDatabase db;

	public DBConnect(Context context) {
		this.context = context;
		dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
	}

	/** Close the database */
	public void close() {
		if (db != null) {
			db.close();
			db = null;
		}
	}

	/** Open the database */
	public void open() throws SQLiteException {
		dbOpenHelper = new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
		try {
			db = dbOpenHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = dbOpenHelper.getReadableDatabase();
		}
	}

	/**
	 * 
	 * @param sharpness
	 *            0 为标清 , 1 为高清, 2为超清
	 * @param quckResolve
	 *            0为否 (不启用), 1为是(启用)
	 * @param wifiOnly
	 *            0为否 (不启用), 1为是(启用)
	 * @return
	 */

	public long insert(int sharpness, int quckResolve, int wifiOnly) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_NAME, "sharpness");
		newValues.put(KEY_VALUE, sharpness);
		if (-1 == db.insert(DB_TABLE, null, newValues)) {
			return -1;
		}
		newValues.put(KEY_NAME, "quick_resolve");
		newValues.put(KEY_VALUE, quckResolve);
		if (-1 == db.insert(DB_TABLE, null, newValues)) {
			return -1;
		}
		newValues.put(KEY_NAME, "wifi_only");
		newValues.put(KEY_VALUE, wifiOnly);
		if (-1 == db.insert(DB_TABLE, null, newValues)) {
			return -1;
		}
		newValues.put(KEY_NAME, "count");
		newValues.put(KEY_VALUE, 1);

		return db.insert(DB_TABLE, null, newValues);
	}

	public long addCount() {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_NAME, "count");
		newValues.put(KEY_VALUE, this.queryOneData(DB_GET_SHARPNESS) + 1);
		return db.update(DB_TABLE, newValues, DB_GET_SHARPNESS, null);
	}

	public long updateValue(String name, int value) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_NAME, name);
		newValues.put(KEY_VALUE, value);
		return db.update(DB_TABLE, newValues, "name = '" + name + "'", null);
	}

	public long insertPlayHistory(String title, String vid, String playTime,
			String duration, String img) {
		String SQL = "insert into history(title,vid,playtime,duration,img) values('"
				+ title
				+ "','"
				+ vid
				+ "','"
				+ playTime
				+ "','"
				+ duration
				+ "','" + img + "')";
		try {
			db.execSQL(SQL);
			return 1;
		} catch (Exception e) {
			Log.e("historyException", e.toString());
			return -1;
		}

	}

	public int queryOneData(String colume) {
		Cursor results = db.query(DB_TABLE, new String[] { KEY_ID, KEY_NAME,
				KEY_VALUE }, colume, null, null, null, null);
		return ConvertToPeople(results);
	}

	public List<YoukuVideo> queryPlayHistoryData() {
		String SQL = "Select * from history group by vid order by _id DESC";
		Cursor results = db.rawQuery(SQL, null);
//				Query(true,DB_HISTORY_TABLE, new String[] {
//				HISTORY_KEY_TITLE, HISTORY_KEY_VID, HISTORY_KEY_IMG,
//				HISTORY_KEY_PLAYTIME, HISTORY_KEY_DURATION }, null, null, null,
//				null, null,null);
		return ConvertToYoukuVideo(results);
	}

	private List<YoukuVideo> ConvertToYoukuVideo(Cursor cursor) {
		int resultCounts = cursor.getCount();
		if (resultCounts == 0 || !cursor.moveToFirst()) {
			return null;
		}
		List<YoukuVideo> historyList = new ArrayList<YoukuVideo>();
		YoukuVideo y;
		for (int i = 0; i < resultCounts; i++) {
			y = new YoukuVideo(cursor.getString(cursor
					.getColumnIndex(HISTORY_KEY_VID)), cursor.getString(cursor
					.getColumnIndex(HISTORY_KEY_TITLE)), cursor.getString(cursor
					.getColumnIndex(HISTORY_KEY_IMG)), "", "");
			cursor.moveToNext();
			historyList.add(y);
		}
		return historyList;
	}

	private int ConvertToPeople(Cursor cursor) {
		int resultCounts = cursor.getCount();
		if (resultCounts == 0 || !cursor.moveToFirst()) {
			return (Integer) null;
		}
		int peoples = 0;
		for (int i = 0; i < resultCounts; i++) {
			peoples = cursor.getInt(cursor.getColumnIndex(KEY_VALUE));

			cursor.moveToNext();
		}
		return peoples;
	}
}
